package teacher.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import teacher.bean.Project;
import teacher.service.project;
import tools.StringDate;

import com.opensymphony.xwork2.ActionSupport;

public class modifyPro2 extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private File fileCom;
	private String fileComFileName;
	private String uploadPath;
	private String FilePath;
	
	private Project proj;
	private int proid;
	private int teaid;
	private String upname;
	private String dates;
	private String dates2;
	String suc;
	
	public String getUpname() {
		return upname;
	}

	public void setUpname(String upname) {
		this.upname = upname;
	}

	public String getDates2() {
		return dates2;
	}

	public void setDates2(String dates2) {
		this.dates2 = dates2;
	}

	public File getFileCom() {
		return fileCom;
	}

	public void setFileCom(File fileCom) {
		this.fileCom = fileCom;
	}

	public String getFileComFileName() {
		return fileComFileName;
	}

	public void setFileComFileName(String fileComFileName) {
		this.fileComFileName = fileComFileName;
	}

	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}

	public int getTeaid() {
		return teaid;
	}

	public void setTeaid(int teaid) {
		this.teaid = teaid;
	}

	public String getDates() {
		return dates;
	}

	public void setDates(String dates) {
		this.dates = dates;
	}
	
	public Project getProj() {
		return proj;
	}

	public void setProj(Project proj) {
		this.proj = proj;
	}

	public int getProid() {
		return proid;
	}

	public void setProid(int proid) {
		this.proid = proid;
	}

	public String execute() throws Exception{
		
		if(fileCom!=null)
		{
			InputStream is = new FileInputStream(fileCom);

			uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");

			File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
	            FilePath=uploadPath+File.separator+this.getFileComFileName();

	     	    File toFile = new File(uploadPath, this.getFileComFileName());

	     		OutputStream os = new FileOutputStream(toFile);

	     		byte[] buffer = new byte[4096];

	     		int length = 0;

	     		while ((length = is.read(buffer)) > 0) {
	     			os.write(buffer, 0, length);
	     		}
	     		System.out.println("上传文件名"+fileComFileName);
	     		System.out.println(FilePath);

	     		is.close();

	     		os.close();
			
	     		proj.setUpload(fileComFileName);
		}else{
			proj.setUpload(upname);
		}
		
	
		
	     proj.setProId(proid);
	     proj.setState(0);
	     proj.setTeaId(teaid);
		 Date d=new StringDate().StringToDate(dates);
		 Date d2=new StringDate().StringToDate(dates2);
		 proj.setBeginDate(d);
		 proj.setFinishDate(d2);
		 
		 if(proj.getArriveFunds()==null){
				proj.setArriveFunds(0);
			}
			if(proj.getLowDepartment()==null){
				proj.setLowDepartment("0");
			}
			if(proj.getLevel()==null){
				proj.setLevel(0);
			}
	
		if(new project().updateProject(proj, proid)){
			this.setSuc("success2");
			return SUCCESS;
		}
		return ERROR;
	}

}
